import Local_Window_ItemCategory from './windowItemCategory.js'
let SWDA_ItemScene = {};
//=============================================================================
// Scene_Item
//=============================================================================

Yanfly.Item.Scene_Item_Create = Scene_Item.prototype.create
Scene_Item.prototype.create = function () {
    Yanfly.Item.Scene_Item_Create.call(this)
}

// 顶部分类菜单
Yanfly.Item.CategoryWindow = Scene_Item.prototype.createCategoryWindow
Scene_Item.prototype.createCategoryWindow = function () {
    // Yanfly.Item.CategoryWindow.call(this)
    this._categoryWindow = new Local_Window_ItemCategory();
    this._categoryWindow.setHelpWindow(this._helpWindow);
    //this._categoryWindow.setHandler('ok',     this.onCategoryOk.bind(this));
    this._categoryWindow.setHandler('cancel', this.popScene.bind(this));    //老饕：这里有问题，选择目标时点取消会导致直接退出物品使用界面。
    this.addWindow(this._categoryWindow);


    // 设置外观基础值
    const [x, y, width] = [650, 40, 556];
    this._categoryWindow.setMaxCols(6);
    this._categoryWindow.x = x;
    this._categoryWindow.y = y;
    this._categoryWindow.width = width

    // 关联行为
    //this._categoryWindow.filterRules = (item) => !['Equips','Weapons', 'Armors'].includes(item.trim())
    this._categoryWindow.filterRules = (item) => !['Weapons', 'Armors'].includes(item.trim())
    this._categoryWindow.refresh()
};


// 详细说明菜单
Yanfly.Item.createHelpWindow = Scene_Item.prototype.createHelpWindow
Scene_Item.prototype.createHelpWindow = function () {
    Yanfly.Item.createHelpWindow.call(this)
    this._helpWindow.x = 0;
    this._helpWindow.y = Graphics.boxHeight - this._helpWindow.height
}


// 物品列表菜单
Scene_Item.prototype.createItemWindow = function () {
    const [x, y, width, height] = [680, this._categoryWindow.y + 65, 312, 480];
    this._itemWindow = new Window_ItemList(x, y, width, height);
    this._itemWindow.width = 482
    this._itemWindow.opacity = 0
    this._itemWindow._scrollbar.setOffset(5)
    this._categoryWindow.setItemWindow(this._itemWindow);
    this.listenItemWindowEvents()
    this.addWindow(this._itemWindow);
    this._itemWindow.activate();
    this._categoryWindow.selectLast();
    //this._itemWindow.selectLast();
}


Scene_Item.prototype.listenItemWindowEvents = function () {
    this._itemWindow.setHelpWindow(this._helpWindow);
    this._itemWindow.setHandler('ok', this.onItemOk.bind(this));
    this._itemWindow.setHandler('cancel', this.onItemCancel.bind(this));
    this._itemWindow.$on('select', this.updateItemPreviewWindow.bind(this));
    this._itemWindow.$on('deselect', this.hideItemPreviewBox.bind(this));
}

/*
Scene_Item.prototype.onCategoryOk = function () {
    if (this._itemWindow) {
        this._itemWindow.selectLast();
    }
    this._categoryWindow.refresh();
};
*/
Scene_Item.prototype.updateItemPreviewWindow = function (id) {
    if (!this._itemWindow) return;
    this._itemWindow.showView()
    this._itemWindow.showPetInfo()
}

Scene_Item.prototype.hideItemPreviewBox = function () {
    if (!this._itemWindow) return;
    this._itemWindow.hideView()
    this._itemWindow.hidePetInfo()
}

Scene_Item.prototype.setActorWindowPosition = function () {
    const _window = this._actorWindow
    _window.x = 0.2 * Graphics.boxWidth
    _window.y = 0.4 * Graphics.boxHeight
}


Scene_Item.prototype.onItemOk = function () {
    const item = this.item()
    if (!item || !$gameParty.canUse(item)) {
        this._itemWindow.activate()
        return
    }

    $gameParty.setLastItem(item)
    this.determineItem() // direct useItem or show actorWindow
    this.setActorWindowPosition()
    this._categoryWindow.deactivate();
};



SWDA_ItemScene.Scene_Item_onItemCancel = Scene_Item.prototype.onItemCancel;
Scene_Item.prototype.onItemCancel = function() {
    SWDA_ItemScene.Scene_Item_onItemCancel.call(this);
    this._categoryWindow.activate();
};

SWDA_ItemScene.Scene_Item_onActorOk = Scene_Item.prototype.onActorOk;
Scene_Item.prototype.onActorOk = function() {
    SWDA_ItemScene.Scene_Item_onActorOk.call(this);
    /*
    if (this.canUse()) {
        this._categoryWindow.activate();
    }
    */
};

SWDA_ItemScene.Scene_Item_onActorOCancel = Scene_Item.prototype.onActorCancel;
Scene_Item.prototype.onActorCancel = function() {
    SWDA_ItemScene.Scene_Item_onActorOCancel.call(this);
    this._categoryWindow.activate();
};



//=============================================================================
// Scene_Item.Window_Help
//=============================================================================
Yanfly.Item.WindowHelp = Window_Help.prototype.initialize
Window_Help.prototype.initialize = function () {
    Yanfly.Item.WindowHelp.call(this, ...arguments)
}
